package com.orphan.util.mqConfig;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;
@Configuration
public class UserDetailInfoMq {
    //创建队列

    //投递失败的死信队列
    @Bean
    public Queue deadQueue1(){
        return new Queue("deadQueue1");
    }

    //要投递的目标队列。
    @Bean
    public Queue userDetailQueue(){
        //关联死信交换机
        Map<String, Object> args = new HashMap<>(3);
        // x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", "deadExchange1");
        // x-dead-letter-routing-key  这里声明当前队列的死信路由key
        args.put("x-dead-letter-routing-key","deadKey1");
        // x-message-ttl  声明队列的TTL
        args.put("x-message-ttl",5000);
        return QueueBuilder.durable("userDetailQueue").withArguments(args).build();
    };

    //创建交换机

    //目标队列的交换机
    @Bean
    public DirectExchange userDetailExchange(){
        return new DirectExchange("userDetailExchange");
    }
    //死信交换机。可以是那4种的任意一种
    @Bean
    public DirectExchange deadExchange1(){
        return new DirectExchange("deadExchange1");
    }

    //把队列绑定到交换机


    //绑定目标队列和交换机
    @Bean
    public Binding userDetailQueueToUserDetailExchange(Queue userDetailQueue, DirectExchange userDetailExchange){
        return BindingBuilder.bind(userDetailQueue).to(userDetailExchange).with("userDetailKey");
    }
    //绑定死信队列和交换机
    @Bean
    public Binding deadQueue1ToDeadExchange1(Queue deadQueue1,DirectExchange deadExchange1){
        return   BindingBuilder.bind(deadQueue1).to(deadExchange1).with("deadKey1");
    }




    //创建队列

    //投递失败的死信队列
    @Bean
    public Queue deadQueue2(){
        return new Queue("deadQueue2");
    }

    //要投递的目标队列。
    @Bean
    public Queue userDepositQueue(){
        //关联死信交换机
        Map<String, Object> args = new HashMap<>(3);
        // x-dead-letter-exchange    这里声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", "deadExchange2");
        // x-dead-letter-routing-key  这里声明当前队列的死信路由key
        args.put("x-dead-letter-routing-key","deadKey2");
        // x-message-ttl  声明队列的TTL
        args.put("x-message-ttl",5000);
        return QueueBuilder.durable("userDepositQueue").withArguments(args).build();
    };

    //创建交换机

    //目标队列的交换机
    @Bean
    public DirectExchange userDepositExchange(){
        return new DirectExchange("userDepositExchange");
    }
    //死信交换机。可以是那4种的任意一种
    @Bean
    public DirectExchange deadExchange2(){
        return new DirectExchange("deadExchange2");
    }

    //把队列绑定到交换机


    //绑定目标队列和交换机
    @Bean
    public Binding userDepositQueueToUserDepositExchange(Queue userDepositQueue, DirectExchange userDepositExchange){
        return BindingBuilder.bind(userDepositQueue).to(userDepositExchange).with("userDepositKey");
    }
    //绑定死信队列和交换机
    @Bean
    public Binding deadQueue2ToDeadExchange2(Queue deadQueue2,DirectExchange deadExchange2){
        return   BindingBuilder.bind(deadQueue2).to(deadExchange2).with("deadKey2");
    }
}
